<template>
  <div class="index">
    <div class="index-box" id="index1">
      <div class="box" id="box">
        <div class="box-top">
          <div class="z_photo">
            <img src="@/assets/img/tx.jpg" alt="" />
          </div>
          <p class="p1">{{ name }}</p>
          <p class="p2">杭州萧山区十五届政协常委</p>
          <p class="p2">浙江民建互联网专委会委员</p>
          <p class="p2">民建中央信息与创新专委会委员</p>
          <p class="p2">{{ company }}{{ title }}</p>
          <div class="qrcode">
            <!-- <div class="qrcode" id="qrcode"></div> -->
            <div class="qrcode1">
              <img :src="res" alt="" />
            </div>
            <div class="qrcode-tip">（长按二维码保存联系方式到手机通讯录）</div>
          </div>
        </div>
        <div class="message" v-if="show">
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon1.png" alt="" />
            </div>
            <div class="txt">{{ tel }}</div>
          </div>
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon5.png" alt="" />
            </div>
            <div class="txt">{{ desc }}</div>
          </div>
          <!-- <div class="list">
          <div class="icon">
            <img src="@/assets/img/icon11.png" alt="" />
          </div>
          <div class="txt">{{ number }}</div>
        </div> -->
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon2.png" alt="" />
            </div>
            <div class="txt">{{ email }}</div>
          </div>
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon3.png" alt="" />
            </div>
            <div class="txt">{{ web }}</div>
          </div>
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon4.png" alt="" />
            </div>
            <div class="txt" style="font-size: 0.75rem">{{ address }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="btn-box" v-if="show1">
      <div class="btn" @click="download">保存</div>
      <div class="btn" @click="rou">生成名片</div>
    </div>
  </div>
</template>
<script setup>
import html2canvas from "html2canvas";
import QRCode from "qrcode";
import { ref } from "vue";
import router from "@/router";
const tel = ref("13735822550");
const name = ref("高清淼");
const title = ref("董事长");
const company = ref("杭州巨创网络科技有限公司");
// const number = ref("13735822550");
const email = ref("gqm@juchuang.net");
const web = ref("www.juchuang.net");
const address = ref("杭州萧山博学路618号科创中心C座1905/1906室");
const desc = ref("g272562823");
const show = ref("show");
const show1 = ref("show");
const res = ref("");
// 生成图片并保存
const generateQR = async () => {
  try {
    var info =
      "BEGIN:VCARD\n" +
      "FN:" +
      name.value +
      "\n" +
      "ORG:" +
      company.value +
      "\n" +
      "TITLE:" +
      title.value +
      "\n" +
      "WORK:" +
      address.value +
      "\n" +
      "TEL:" +
      tel.value +
      "\n" +
      "EMAIL:" +
      email.value +
      "\n" +
      // "URL:" +
      // web.value +
      // "\n" +
      "NOTE:" +
      desc.value +
      "\n" +
      "END:VCARD";
    res.value = await QRCode.toDataURL(info);
  } catch (err) {
    console.error(err);
  }
};
generateQR();
const download = () => {
  show1.value = false;
  setTimeout(() => {
    let canvasID = document.getElementById("index1"); // 获取要生成图片的页面元素
    html2canvas(canvasID, {
      scale: 6, //数值越大生成的图片越清晰
      backgroundColor: null, //解决生成的图片有白边
    }).then((canvas) => {
      let dataURL = canvas.toDataURL("image/png");
      // uploadFile(dataURL);
      // saveFile(dataURL, "图片名称");
      var url = canvas.toDataURL("image/png"); //得到图片的base64编码数据'
      var a = document.createElement("a"); // 生成一个a元素
      var event = new MouseEvent("click", {
        bubbles: true,
        cancelable: true,
        view: window,
      }); // 创建一个单击事件
      a.download = "photo"; // 设置图片名称
      a.href = url; // 将生成的URL设置为a.href属性
      a.dispatchEvent(event); // 触发a的单击事件
    });
  }, 500);
};
// 跳转生成我的名片
const rou = () => {
  router.push({
    path: "/indexs",
  });
};
</script>
<style lang="less" scoped>
.index {
  width: 100vw;
  min-height: 100vh;
  background-color: #0e87e2;
  .index-box {
    background-color: #0e87e2;
    padding: 1.25rem 1rem;
    .box {
      background-color: #f9f9f9;
      border-radius: 4px 4px 0 0;
      .box-top {
        text-align: center;
        border-radius: 4px 4px 0 0;
        padding: 0.3125rem 0rem 10px 0;
        background: #fff url(@/assets/img/title.png) no-repeat top center/100%
          40px;
        .z_photo {
          display: block;
          width: 100px;
          height: 100px;
          margin: 0 auto;
          border: 4px solid #fff;
          overflow: hidden;
          border-radius: 50%;
          img {
            width: 100%;
          }
        }
        .p1 {
          font-weight: bold;
          font-size: 1.5rem;
          color: #333;
          line-height: 2.5rem;
        }
        .p2 {
          font-size: 1rem;
          color: #666;
          line-height: 1.5rem;
          letter-spacing: 0.0625rem;
        }
      }
      // 二维码
      .qrcode1 img {
        display: block;
        width: 8.75rem;
        height: 8.75rem;
        margin: auto;
      }

      .qrcode-tip {
        text-align: center;
        color: #333;
        font-size: 0.8rem;
        color: #f96774;
      }
      //信息
      .message {
        width: 100%;
        padding: 0rem 1rem;
        box-sizing: border-box;
        .list {
          display: flex;
          align-items: center;
          padding: 0.5rem 0.3125rem;
          border-bottom: 0.0625rem solid #e4e4e4;
          .icon {
            width: 1.75rem;
            height: 1.75rem;
            img {
              width: 100%;
              font-size: 0;
              display: block;
            }
          }
          .txt {
            flex: 1;
            margin-left: 0.8125rem;
            font-size: 16px;
            color: #333;
          }
        }
      }
    }
  }

  .btn-box {
    width: 100%;
    padding: 0rem 1rem 1rem 1rem;
    display: flex;
    align-items: center;
    .btn {
      background: #0e87e2;
      width: 100%;
      height: 40px;
      background: #ddeefc;
      line-height: 40px;
      text-align: center;
      border-radius: 2px;
      font-size: 17px;
      color: #0b75d0;
      font-weight: bold;
      letter-spacing: 4px;
    }
    .btn:nth-child(2) {
      margin-left: 1.25rem;
    }
  }
}
</style>
